<template>
    <div class="caretaker-manage-container">
        <h2 class="title">护工管理</h2>

        <!-- 查询栏 -->
        <el-form :inline="true" :model="searchForm" class="search-form">
            <el-form-item label="管家名：">
                <el-input v-model="searchForm.username" placeholder="请输入护工姓名" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleSearch">查询</el-button>
                <el-button type="default" @click="handleReset">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 表格 -->
        <el-table :data="tableData" v-loading="loading" border style="width: 100%; background-color: #f5f7fa;"
            :header-cell-style="{ background: '#f9fafc', color: '#333', textAlign: 'center' }"
            :cell-style="{ textAlign: 'center' }">
            <el-table-column prop="username" label="姓名" />
            <el-table-column prop="phone" label="电话" />
            <el-table-column prop="role" label="角色" />
            <el-table-column label="操作" width="180">
                <template #default="scope">
                    <el-button type="primary" size="small" @click="openServiceDialog(scope.row)">设置服务</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-bar">
            <el-pagination v-model:current-page="pagination.cur" v-model:page-size="pagination.size"
                :page-sizes="[5, 10, 20]" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
                @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
    </div>

    <SetCaretakerClientsDialog ref="serviceDialogRef" />
</template>

<script setup>
import { ref, onMounted } from 'vue'
import {inject} from 'vue'
const axios = inject("axios")
import { ElMessage } from 'element-plus'
import SetCaretakerClientsDialog from '@/components/caretaker/caretakerManage/SetCaretakerClientsDialog.vue'

const searchForm = ref({ username: '' })
const serviceDialogRef = ref()

const tableData = ref([])
const loading = ref(false)
const pagination = ref({
    cur: 1,
    size: 10,
    total: 0
})

const fetchData = async () => {
    loading.value = true
    try {
        const res = await axios.get('/user/pageCaretakersByName', {
            params: {
                cur: pagination.value.cur,
                number: pagination.value.size,
                username: searchForm.value.username || ''
            }
        })
        if (res.data.status === 200) {
            tableData.value = res.data.data || []
            pagination.value.total = res.data.total || 0
        } else {
            ElMessage.warning(res.data.msg || '查询失败')
        }
    } catch (err) {
        ElMessage.error('数据加载失败')
    } finally {
        loading.value = false
    }
}

const handleSearch = () => {
    pagination.value.cur = 1
    fetchData()
}
const handleReset = () => {
    searchForm.value.username = ''
    pagination.value.cur = 1
    fetchData()
}
const handleSizeChange = (size) => {
    pagination.value.size = size
    pagination.value.cur = 1
    fetchData()
}
const handleCurrentChange = (page) => {
    pagination.value.cur = page
    fetchData()
}
const openServiceDialog = (row) => {
    serviceDialogRef.value.open(row.userId, row.username)
}
onMounted(fetchData)
</script>

<style scoped>
.caretaker-manage-container {
    padding: 20px;
}

.title {
    font-size: 27.2px;
    font-weight: bold;
    margin: 0 0 20px 0;
    color: #333;
}

.search-form {
    margin-bottom: 20px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.pagination-bar {
    display: flex;
    justify-content: flex-end;
    margin-top: 2%;
    margin-bottom: 2%;
}
</style>
